package com.hzdp.handles.category;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hzdp.dao.RivalCategoryDao;
import com.hzdp.dao.RivalProductDao;
import com.hzdp.dao.po.RivalCategoryPO;
import com.hzdp.dao.po.RivalProductPO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lyp on 15/12/26.
 */
@Component("categoryProcessor")
public class CategoryProcessor {

    @Autowired
    private RivalProductDao rivalProductDao;
    @Autowired
    private RivalCategoryDao rivalCategoryDao;

    private final Logger logger = Logger.getLogger(this.getClass());

    private static final String CATEGORY1  = "类型";
    private static final String CATEGORY2  = "类别";


    public List<String> getAllCompanyCategories(int companyId){
        try {
            return rivalProductDao.getAllCompanyCategories(companyId);
        } catch (SQLException e) {
            logger.error(e);
            return null;
        }
    }

    public void hanld(int companyId,String companyCategory){
        try {
            Integer count = rivalProductDao.getProductCount(companyId,companyCategory);
            if(count<=0){
                return;
            }

            int sizePerTime = 500;

            for(int i=0;i<count;i+=sizePerTime){
                List<RivalProductPO> products = rivalProductDao.getProducts(companyId, companyCategory, i, sizePerTime);
                if(CollectionUtils.isEmpty(products)){
                    return;
                }

                for(RivalProductPO each : products){
                    try {
                        handleEach(each,companyCategory);
                    } catch (Exception e) {
                        logger.error(e);
                    }
                }
            }
        } catch (SQLException e) {
            logger.error(e);
        }
    }

    private void handleEach(RivalProductPO product, String companyCategory) throws IOException, SQLException {
        String descs = product.getDescs();
        Map<String,String> descMap = new ObjectMapper().readValue(descs, HashMap.class);

        String categoryName2 =  descMap.get(CATEGORY1);
        if(StringUtils.isBlank(categoryName2)){
            categoryName2 = descMap.get(CATEGORY2);
        }

        if(StringUtils.isBlank(categoryName2)){
            logger.error("can't find categoryName2 from the product, rivalProduct id="+product.getId());
            return;
        }

        RivalCategoryPO rivalCategory = rivalCategoryDao.getRivalCategoryByName(companyCategory, categoryName2);
        int id;
        if(rivalCategory!=null){
            id = rivalCategory.getId();
        }else{
            RivalCategoryPO rivalCategoryPO = new RivalCategoryPO();
            rivalCategoryPO.setCategoryName1(companyCategory);
            rivalCategoryPO.setCategoryName2(categoryName2);
            Integer result = rivalCategoryDao.insert(rivalCategoryPO);
            if(result==null || result<=0){
                logger.error("insert rivalCategory failed , categoryName1:"+companyCategory
                +", categoryName2"+categoryName2);
                return;
            }
            id = result;
        }
        rivalProductDao.updateRivalCategoryId(product.getId(),id);
    }
}
